package com.carpedil.order.controller;

import com.carpedil.auth.service.FeiginAuthenticService;
import com.carpedil.common.dto.OrderDTO;
import com.carpedil.common.dto.UserDTO;
import com.carpedil.order.entity.Order;
import com.carpedil.order.service.OrderService;
import com.carpedil.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** @author Administrator */
@Controller
@RequestMapping("/order")
public class OrderController {
  @Autowired OrderService orderService;
  @Autowired FeiginAuthenticService feiginAuthenticService;

  @PostMapping("/new_order")
  @ResponseBody
  public String createOrder(@RequestBody OrderDTO dto) {
    return orderService.save(dto);
  }

  @GetMapping("/update_status/{orderId}/{code}")
  @ResponseBody
  public Integer updateOrderStatus(
      @PathVariable("orderId") String orderId, @PathVariable("code") Integer code) {
    return orderService.updateStatusByName(orderId, code);
  }

  @GetMapping("/orders")
  @ResponseBody
  public List<Order> getOrderList() {
    return orderService.getOrderList();
  }

  @GetMapping("/info")
  @ResponseBody
  public String info() {
    return "hello feigin";
  }

  @GetMapping("/toLogin")
  @ResponseBody
  public String toAuth() {
    return feiginAuthenticService.toLogin();
  }

  @GetMapping({"/", "/index/{token}"})
  public String toIndex(@PathVariable("token") String token) {
    if (null != token) {
      return "index";
    }
    return "error";
  }

  @GetMapping("/token/{token}")
  public String toverifierJwt(@PathVariable("token") String token) {
    //
    UserDTO dto = JwtUtil.verifierJwt(token);
    if (dto != null) {
      return this.toIndex(token);
    } else {
      return "error";
    }
  }
}
